<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="合同商品明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <div>
      <salesContractMx ref="salesContractMx"></salesContractMx>
    </div>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="onSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
  <script>
import VolBox from "@/components/basic/VolBox.vue";
import salesContractMx from "@/views/salesinfo/sales_contract_mx/q_sales_contract_mx.vue";
import { mapMutations } from "vuex";
import { onUnmounted } from "vue";
//这里使用的vue2语法，也可以写成vue3语法
export default {
  setup() {
    onUnmounted(() => {
      console.log("2页面销毁");
    });
  },
  components: {
    "vol-box": VolBox,
    salesContractMx,
  },
  data() {
    return {
      model: false,
    };
  },
  watch: {
    model(val) {
      console.log("model", val);
    },
  },
  beforeDestroy() {
    console.log("页面销毁");
  },
  methods: {
    ...mapMutations(
      ("salesContractMX", ["storeChangeForm", "storeRemoveForm"])
    ),
    async onModelClose() {
      this.$refs.salesContractMx.searchFormFields.单号ID = null;
      this.$refs.salesContractMx.searchFormFields.单号 = null;
      this.$refs.salesContractMx.searchFormFields.客户 = null;
      this.$refs.salesContractMx.searchFormFields.编码 = null;
      this.$refs.salesContractMx.searchFormFields.发货状态 = null;
      this.$refs.salesContractMx.searchFormFields.签订日期 = ["", ""];
      this.$refs.salesContractMx.searchFormFields.交付日期 = ["", ""];
      // if (this.$route.path == "/q_sales") {
      //   this.storeRemoveForm();
      // }

      //   await this.$refs.salesContractMx.$refs.grid.search();
    },
    open(ID, Voucher_sub) {
      this.model = true;
      let form = {};
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.salesContractMx) {
          clearInterval(setIntervalTiem);
          //#region
          //   let now = new Date();
          //   let year = now.getFullYear();
          //   let month =
          //     now.getMonth() + 1 >= 10
          //       ? now.getMonth() + 1
          //       : "0" + (now.getMonth() + 1); // 注意：月份从0开始，需要加1
          //   let day = now.getDate() >= 10 ? now.getDate() : "0" + now.getDate();
          // this.$refs.salesContractMx.searchFormFields.Delivery_time = [year+'-'+month+'-'+day, ""];
          //#endregion

          // this.$refs.salesContractMx.searchFormFields.单号ID = ID;
          // if (this.$route.path == "/q_sales") {
          //   form.发货状态 = 2;
          //   this.storeChangeForm(form);
          //   this.$refs.salesContractMx.searchFormFields.发货状态 = "2";
          // } else
          if (this.$route.path == "/q_delivery_request") {
            // form.发货状态 = 0;
            // this.storeChangeForm(form);
            this.$refs.salesContractMx.searchFormFields.发货状态 = "0";
          } else if (this.$route.path == "/q_install_request") {
            // form.发货状态 = 1;
            // this.storeChangeForm(form);
            // this.$refs.salesContractMx.searchFormFields.发货状态 = "1";
            // this.$refs.salesContractMx.searchFormFields.单号 = Voucher_sub;
          }
          // this.$refs.salesContractMx.searchFormFields.单号 = Voucher_sub;
          // this.$refs.salesContractMx.$refs.grid.search(); //引用页面,每次打开都触发搜索
          //   this.$refs.salesContractMx.$refs.grid.searchBefore(1)
          // this.$refs.salesContractMx.$refs.grid.searchFormOptions.forEach(
          //   (item) => {
          //     item.forEach((x) => {
          //       if (x.field == "单号") {
          //         x.disabled = true;
          //       }
          //     });
          //   }
          // );
          this.$refs.salesContractMx.$refs.grid.search();
        }
      }, 0);
    },

    onSelect() {
      let rowObj = this.$refs.salesContractMx.$refs.grid.getSelectRows();
      if (!rowObj.length) {
        this.base.ElMessageFn("请选择一条数据");
        return (this.model = true);
      }
      let some = null;
      let msgString = "";
      if (this.$route.path == "/q_sales") {
        // 调试后放开 test
        // some = rowObj.some((item) => item.发货状态 == 0);
        // msgString = "请勿选择未发货商品";
      } else if (this.$route.path == "/q_delivery_request") {
        some = rowObj.some((item) => item.发货状态 != 0);
        msgString = "请选择未发货商品";
      } else if (this.$route.path == "/q_install_request") {
        // 调试后放开 test
        // some = rowObj.some((item) => item.发货状态 != 1);
        // msgString = "请选择发货完成商品";
          // let installSome = rowObj.some((item) => item.安装状态 != 0);
          // console.log("installSome", installSome);
          // if (installSome) {
          //   this.base.ElMessageFn("请选择未安装商品");
          //   return;
          // }
      }
      if (some) {
        this.base.ElMessageFn(msgString);
        return;
      }
      this.$emit("parentCall", ($parent) => {
        let arr = [];
        if (
          this.$route.path == "/q_delivery_request" 
          // ||this.$route.path == "/q_install_request"
        ) {
          arr = rowObj.map((item) => {
            return {
              Goods_sub: item.编码,
              Goods_name: item.名称,
              Contract_goods_num: item.未销数量,
              Goods_num: item.未销数量,
              Goods_size: item.规格,
              Goods_price: item.单价,
              Total_price: item.总价,
              Logistics_status: item.发货状态,
              Install_status: item.安装状态,
              sales_contractList_ID: item.ID,
              Sales_contract_voucher_sub: item.单号,
            };
          });
          $parent.$refs.detail.rowData.unshift(...arr);
        } 
        // else if (this.$route.path == "/q_sales") {
        //   arr = rowObj.map((item) => {
        //     return {
        //       Goods_sub: item.编码,
        //       Goods_name: item.名称,
        //       Goods_num: item.数量,
        //       Goods_size: item.规格,
        //       Goods_price: item.单价,
        //       Total_price: item.总价,
        //       Logistics_status: item.发货状态,
        //       Install_status: item.安装状态,
        //       sales_contractList_ID: item.ID,
        //       Sales_contract_voucher_sub: item.单号,
        //     };
        //   });
        //   $parent.$refs.detail.rowData.unshift(...arr);
        // }
      });
      this.onModelClose();
      return (this.model = false);
    },
  },
};
</script>